/**
* ...
* @author Default
* @version 0.1
*/

package org.anivision.forms {
	import flash.events.*;
	import org.anivision.document.Document;
	import org.anivision.r3d.materials.WireframeMaterial2;
	import org.anivision.document.MeshDocument;
	import org.aswing.ASColor;
	import org.aswing.BorderLayout;
	import org.aswing.JCheckBoxMenuItem;
	import org.aswing.JMenu;
	import org.aswing.JMenuBar;
	import org.aswing.JPanel;
	import org.aswing.JSeparator;
	import org.papervision3d.cameras.*;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.PaperPlane;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.Viewport3D;

	public class R3DViewer extends BaseViewer {
		
		protected var d_view:Viewport3D;
		protected var d_camera:Camera3D;
		protected var d_renderer:BasicRenderEngine;
		protected var d_scene:Scene3D;
		protected var d_plane:Plane;
		
		protected var d_mainMenu:JMenuBar;
		protected var d_viewMenu:JMenu;
		protected var d_cameraMenu:JMenu;
		protected var d_cameraMenu_targetMenu:JMenu;
		protected var d_targetMenu_lockCheck:JCheckBoxMenuItem;
				
		public function R3DViewer(doc:MeshDocument) {
			super(doc);
			init();
		}
		
		public override function init():void {
			setBackground(new ASColor(0x555555, 1));
			setOpaque(true);
			
			d_view = new Viewport3D(getWidth(), getHeight(), false, false, true, true);
			d_camera = new Camera3D(null, 3, 400);
			d_camera.x = 0;
			d_camera.y = 300;
			d_camera.z = 0;
			d_camera.rotationX = -45;			
			
			d_renderer = new BasicRenderEngine();
			d_scene = new Scene3D();
			
			addChild(d_view.containerSprite);
			
			d_plane = new Plane(new WireframeMaterial2(0xffffff, 50, 2), 2000, 2000, 10, 10, { x:0, y: 0, z: 0, rotationX: -90});
			d_scene.addChild(d_plane, 'plane');
						
			addEventListener(Event.ENTER_FRAME, onRenderTick);
			
			initUI();
		}
		
		public function initUI(): void {
			setLayout(new BorderLayout());
			
			// Setup Main menu
			d_mainMenu = new JMenuBar();
			d_mainMenu.setConstraints('North');
			d_mainMenu.setOpaque(true);
			
			// top level menus
			d_viewMenu = new JMenu('View');
			d_cameraMenu = new JMenu('Camera');
			
			// second level menus
			d_cameraMenu_targetMenu = new JMenu('Target');
			
			// third level menus
			d_targetMenu_lockCheck = new JCheckBoxMenuItem('Lock on Selection');
			d_targetMenu_lockCheck.setSelected(true);
			
			var sep01:JSeparator = new JSeparator();
			
			// add third level menus
			d_cameraMenu_targetMenu.append(d_targetMenu_lockCheck);
			d_cameraMenu_targetMenu.append(sep01);
			
			// add second level menus
			d_cameraMenu.append(d_cameraMenu_targetMenu);

			// add top level menus to main menu
			d_mainMenu.addMenu(d_viewMenu);
			d_mainMenu.addMenu(d_cameraMenu);

			append(d_mainMenu);			
		}
		
		protected function checkSize():void {
			if ( d_view.viewportWidth != getWidth() + 50 || d_view.viewportHeight != getHeight() + 50 )
			{
				d_view.viewportWidth = getWidth() + 50;
				d_view.viewportHeight = getHeight() + 50;
			}
		}

		protected function onRenderTick(event:Event=null):void {			
            d_camera.x += ( ( (stage.mouseX - (stage.stageWidth * .5)) * 2 ) - d_camera.x ) * .05;
            d_camera.y += ( ( (stage.mouseY-(stage.stageHeight*.5)) *.5) - d_camera.y ) * .03;

			checkSize();
			d_camera.lookAt(d_plane, new Number3D(0, 1, 0));
			d_renderer.renderScene(d_scene, d_camera, d_view);
		}

	}
	
}
